/*
 * 	获取 图片查看器 的实例
 * 	定义图片名格式为 prefix + index + suffix
 * 	即前缀+编号+后缀，编号无前导0
 * 	如  img1.jpg ， 前缀为 'img'，后缀为 '.jpg'
 *	URL：图片所在的文件夹
 * 	prefix：图片名的前缀
 *  suffix：图片名的后缀
 * 	currentIndex：当前图片的编号
 * 	begin：图片序列中第一张图片的编号 （建议为1）
 * 	end：最后一张图片的编号
 */

function getImageViewer(URL, prefix, suffix, currentIndex, begin, end) {
	this.url = URL;
	this.prefix = prefix;
	this.suffix = suffix;
	this.currentIndex = currentIndex;
	this.begin = begin;
	this.end = end;
	this.imgContent = document.getElementById("id_imgContent"); // img 标签

	this.hasNext = function() { // 有上一张图片
		return this.currentIndex < this.end;
	};
	this.hasPrevious = function() { // 有下一张图片
		return this.currentIndex > this.begin;
	};
	this.previousImg = function() { // 显示上一张图片
		console.log('left,' + getURL());
		if(hasPrevious()) {
			//			console.log('has pre');
			this.currentIndex -= 1;
		} else {
			mui.toast('已经是第一张了');
		}
		init();
	};
	this.nextImg = function() { // 显示下一张图片
		console.log('next,' + getURL());
		if(hasNext()) {
			this.currentIndex += 1;
		} else {
			mui.toast('已经是最后一张了');
		}
		init();
	};
	this.getURL = function() { // 获取当前图片的 url
		var url = URL + "/" + this.prefix + this.currentIndex + this.suffix;
		console.log('url=' + url);
		return url;
	};
	this.downloadImg = function() { //	下载当前图片
		mui.toast("图片下载成功");
		var downLoader = plus.downloader.createDownload(getURL(), {
			method: 'GET',
			filename: this.index + this.suffix
		}, function(download, status) {
			var fileName = download.filename;
			console.log('文件名:' + fileName);
			console.log('下载状态：' + status);
			/**
			 * 保存至本地相册
			 * http://www.html5plus.org/doc/zh_cn/gallery.html#plus.gallery.save
			 */
			plus.gallery.save(fileName, function() {
				/**
				 * 保存后，弹出对话框是否查看；
				 * http://dev.dcloud.net.cn/mui/ui/#dialog
				 */
				mui.confirm("打开相册", "打开相册？", ["打开", "不看"], function(event) {
					var gindex = event.index;
					if(gindex == 0) {
						/**
						 * 选择图片
						 * http://www.html5plus.org/doc/zh_cn/gallery.html#plus.gallery.pick
						 */
						plus.gallery.pick(function(file) {
							mui.toast("你选择了图片：" + file);
						}, function(error) {
							console.log(error);
						}, {

						});
					}
				});
			});
		});
		/**
		 * 开始下载任务
		 * http://www.html5plus.org/doc/zh_cn/downloader.html#plus.downloader.Download.start
		 */
		downLoader.start();

	};
	this.like = function() { //	收藏当前图片
		console.log('收藏');
		window.localStorage.setItem(getURL(), 'like');
		document.getElementById("id_disliked").style.display = 'none';
		document.getElementById("id_liked").style.display = '';
	};
	this.dislike = function() { //	取消收藏 当前图片
		window.localStorage.setItem(getURL(), null);
		document.getElementById("id_disliked").style.display = '';
		document.getElementById("id_liked").style.display = 'none';
	};
	this.isLiked = function() { //	是否被收藏
		var x = window.localStorage.getItem(getURL());
		return x == 'like';
	};
	this.init = function() {
		document.getElementById('id_imgContent').src = getURL();
		var liked = isLiked();
		mui.toast(liked);

		if(liked) {
			document.getElementById("id_disliked").style.display = '';
			document.getElementById("id_liked").style.display = 'none';
		} else {
			document.getElementById("id_disliked").style.display = 'none';
			document.getElementById("id_liked").style.display = ''
		}

	};
	this.fullScreen = true; //	大图还是小图
	this.setWallpaper = function() { //	设置为壁纸
		console.log('设为壁纸');
		var WallpaperManager = plus.android.importClass("android.app.WallpaperManager");
		var Main = plus.android.runtimeMainActivity();
		var wallpaperManager = WallpaperManager.getInstance(Main);
		plus.android.importClass(wallpaperManager);
		var BitmapFactory = plus.android.importClass("android.graphics.BitmapFactory");
		var url = getURL(); // 换成要设置的壁纸图片路径
		var path = plus.io.convertLocalFileSystemURL(url);
		var bitmap = BitmapFactory.decodeFile(path);
		try {
			wallpaperManager.setBitmap(bitmap);
			bitmap.recycle(); // 设置完毕桌面要进行 原生层的BITMAP回收 减少内存压力
		} catch(e) {
			//TODO handle the exception
		}

	};

	this.changeSize = function() {
		var i = document.getElementById('id_imgContent');
		var h = window.innerHeight,
			w = window.innerWidth
		console.log(w + ',' + h);
		if(this.fullScreen) { // 当前是大图，切换为小图
			i.style.height = '';
			i.style.width = w + 'px';
			i.style.marginTop = (h / 2 - i.height / 2 - 40) + 'px';
			this.fullScreen = false;
		} else {
			i.style.height = h + 'px';
			i.style.width = '';
			i.style.marginTop = 0 + 'px';
			this.fullScreen = true;
		}

	}
	this.imgContent.addEventListener("tap", function(event) {

		changeSize();
	});

	init();
}

document.getElementById('id_btLike').click = function(event) {
	mui.toast('bt_like clicked')
	if(viewer.isLiked()) {
		viewer.dislike();
	} else {
		viewer.like();
	}
}